package com.sgb.kjwl.viewmodel.enterprise;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.api.EnterpriseAPIManager;
import com.sgb.kjwl.model.entity.enterprise.AssesTypeEntiy;
import com.sgb.kjwl.model.entity.enterprise.NeedListItemEntity;
import com.sgb.kjwl.model.entity.enterprise.OldAndNewTypeEntiy;
import com.sgb.kjwl.utils.PointUtils;
import com.swgk.core.util.UserOperating;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.util.CheckUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AddNeed_AssetsModel extends BaseViewModel {

    public MutableLiveData<String> name = new MutableLiveData<>();
    public MutableLiveData<String> assets_type = new MutableLiveData<>();
    public MutableLiveData<Boolean> ishow = new MutableLiveData<>(false);
    public MutableLiveData<String> newandoldtype = new MutableLiveData<>();
    public MutableLiveData<String> money = new MutableLiveData<>();
    public MutableLiveData<Boolean> yijia = new MutableLiveData<>(false);
    public MutableLiveData<String> user = new MutableLiveData<>();
    public MutableLiveData<String> phone = new MutableLiveData<>();
    public MutableLiveData<String> need_content = new MutableLiveData<>();
    public MutableLiveData<String> need_remarks = new MutableLiveData<>();
    public MutableLiveData<String> toast_message = new MutableLiveData<>();
    public List<AssesTypeEntiy> listdata = new ArrayList<>();
    public MutableLiveData<Integer> showloading_diag = new MutableLiveData<>();
    public MutableLiveData<Integer> showchoicepopwindow = new MutableLiveData<>();
    public List<OldAndNewTypeEntiy> oldandnewtype_list = new ArrayList<>();
    public String assets_type_id ="";
    public String ids = "";
    public NeedListItemEntity data = null;


    public void subimtData(){


        if(TextUtils.isEmpty(name.getValue())){
            toast_message.postValue("请输入需求标题！");
            return;
        }


        if(TextUtils.isEmpty(assets_type.getValue())){
            toast_message.postValue("请选择资产类型！");
            return;
        }

        if(ishow.getValue()&&TextUtils.isEmpty(newandoldtype.getValue())){
            toast_message.postValue("请选择新旧程度！");
            return;
        }

        if(TextUtils.isEmpty(money.getValue())&&!yijia.getValue()){
            toast_message.postValue("请输入预算价格！");
            return;
        }

        if(TextUtils.isEmpty(user.getValue())){
            toast_message.postValue("请输入联系人姓名！");

            return;
        }


        if(TextUtils.isEmpty(phone.getValue())){
            toast_message.postValue("请输入联系电话！");
            return;
        }
        if (!CheckUtils.isMobileForService(phone.getValue())){
            toast_message.postValue("请输入正确的电话格式！");
            return;
        }

        if(TextUtils.isEmpty(need_content.getValue())){

            toast_message.postValue("请输入需求说明内容！");
            return;
        }
        Map<String,Object> map = new HashMap<>();
        map.put("demandTitle",name.getValue());

        map.put("assetsType",assets_type_id);
        map.put("typeName",assets_type.getValue());
        if(ishow.getValue()){
            map.put("assetsOldNew",ids);
            map.put("oldNewName",newandoldtype.getValue());
        }else{
            map.put("assetsOldNew","");
            map.put("oldNewName","");
        }

        map.put("budgetPrice",yijia.getValue()?"-1":money.getValue());
        map.put("linkman",user.getValue());
        map.put("linkmanPhone",phone.getValue());

        map.put("requirementTypes",6);
        if(!TextUtils.isEmpty(need_remarks.getValue())){
            map.put("note",need_remarks.getValue());
        }
        map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME,""));
        map.put("userAccount", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, ""));
        map.put("userNo","0");
        map.put("companyNo","0");
        map.put("teamNo","0");
        map.put("publishType",UserOperating.getInstance().isCompany()?1:UserOperating.getInstance().isTeam()?2:0);
        switch ((Integer) map.get("publishType")){
            case 1:
                map.put("companyNo",SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,""));
                map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NAME,""));
                break;
            case 2:
                map.put("teamNo", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NO,""));
                map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.COMPANY_NAME,""));
                break;

            case 0:
                map.put("userNo",SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.USER_NO,""));
                map.put("realName", SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.REAL_NAME,""));
                break;
        }
        map.put("demandType",0);
        map.put("specifications",need_content.getValue());
        showloading_diag.postValue(1);
        if(data!=null){
            map.put("id",data.getId());
            map.put("createUser",data.getCreateUser());
            data.setDemandTitle(name.getValue());
            data.setBudgetPrice(yijia.getValue()?-1:Double.parseDouble(money.getValue()));
            data.setLinkman(name.getValue());
            data.setLinkmanPhone(phone.getValue());
            data.setNote(need_remarks.getValue());
            data.setSpecifications(need_content.getValue());

            data.setAssetsType(map.get("assetsType").toString());
            data.setTypeName(map.get("typeName").toString());
            if(ishow.getValue()){
                data.setAssetsOldNew(ids);
                data.setOldNewName(map.get("oldNewName").toString());
            }else{
                data.setAssetsOldNew("");
                data.setOldNewName("");
            }
            APIManager.getInstance().getNeedAPI().updateNeedMessage(map).enqueue(new Callback<BaseEntity<Object>>() {
                @Override
                public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {
                    if(response!=null&&response.body()!=null) {
                        showloading_diag.postValue(3);
                    }else{
                        showloading_diag.postValue(2);
                        toast_message.postValue(response.message());
                    }

                }

                @Override
                public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                    showloading_diag.postValue(2);
                    toast_message.postValue(t.getMessage());

                }
            });
            return;

        }
        APIManager.getInstance().getNeedAPI().publishNeedMessage(map).enqueue(new Callback<BaseEntity<Object>>() {
            @Override
            public void onResponse(Call<BaseEntity<Object>> call, Response<BaseEntity<Object>> response) {

                if(response!=null&&response.body()!=null) {
                    showloading_diag.postValue(3);
                }else{
                    showloading_diag.postValue(2);
                    toast_message.postValue(response.message());
                }

            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                showloading_diag.postValue(2);
                toast_message.postValue(t.getMessage());

            }
        });

    }

    /**
     * 获取资产类型
     */
    public void getAssesTsType(){
        if(listdata.size()>0){
            showchoicepopwindow.postValue(1);
            return;
        }
        showloading_diag.postValue(1);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().queryPropertyType_2().enqueue(new Callback<BaseEntity<List<AssesTypeEntiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AssesTypeEntiy>>> call, Response<BaseEntity<List<AssesTypeEntiy>>> response) {
                showloading_diag.postValue(2);
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    listdata.addAll(response.body().getData());
                    showchoicepopwindow.postValue(1);
                } else {
                    toast_message.postValue(response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AssesTypeEntiy>>> call, Throwable t) {
                showloading_diag.postValue(2);
            }
        });
    }
    /**
     * 获取新旧资产
     */
    public void getNewAndOldData(){

        if(oldandnewtype_list.size()>0){
            showchoicepopwindow.postValue(2);
            return;
        }
        showloading_diag.postValue(1);
        EnterpriseAPIManager.getInstance().getEnterpriseAPI().getPropertyNewOrOldOption2().enqueue(new Callback<BaseEntity<List<OldAndNewTypeEntiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<OldAndNewTypeEntiy>>> call, Response<BaseEntity<List<OldAndNewTypeEntiy>>> response) {
                if (response.body() == null || response.body().getData() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {

                    oldandnewtype_list.addAll(response.body().getData());
                    showchoicepopwindow.postValue(2);
                } else {

                }
                showloading_diag.postValue(2);
            }

            @Override
            public void onFailure(Call<BaseEntity<List<OldAndNewTypeEntiy>>> call, Throwable t) {
                showloading_diag.postValue(2);
            }
        });
    }

    public void setdata(){
        if(!TextUtils.isEmpty(data.getDemandTitle())){
            name.postValue(data.getDemandTitle());
        }

        if(!TextUtils.isEmpty(data.getTypeName())){
            assets_type.postValue(data.getTypeName());
        }
        if(!TextUtils.isEmpty(data.getAssetsType())){
            assets_type_id = data.getAssetsType();
        }
        if(!TextUtils.isEmpty(data.getAssetsOldNew())){
            ids = data.getAssetsOldNew();
            ishow.postValue(true);
        }else{
            ishow.postValue(false);
        }
        if(!TextUtils.isEmpty(data.getOldNewName())){
            newandoldtype.postValue(data.getOldNewName());
        }

        if(data.getBudgetPrice()>0){
            money.postValue(PointUtils.price(data.getBudgetPrice()));
            yijia.postValue(false);
        }else{
            yijia.postValue(true);
        }

        if(!TextUtils.isEmpty(data.getLinkman())){
            user.postValue(data.getLinkman());
        }
        if(!TextUtils.isEmpty(data.getLinkmanPhone())){
            phone.postValue(data.getLinkmanPhone());
        }
        if(!TextUtils.isEmpty(data.getSpecifications())){
            need_content.postValue(data.getSpecifications());
        }
        if(!TextUtils.isEmpty(data.getNote())){
            need_remarks.postValue(data.getNote());
        }

    }
}
